package com.beerandjesus.sportofkings.handicapper.profile.resolver;

import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.Horse;
import com.beerandjesus.sportofkings.handicapper.model.brisnet.singlefile.HorseRace;
import com.beerandjesus.sportofkings.handicapper.profile.Profile;
import com.beerandjesus.sportofkings.handicapper.profile.ProfileType;
import org.springframework.stereotype.Component;

import static com.beerandjesus.sportofkings.handicapper.profile.ProfileBuilder.analysisProfile;
import static com.beerandjesus.sportofkings.handicapper.profile.ProfileType.PRIME_POWER_EDGE;
import static com.beerandjesus.sportofkings.handicapper.profile.ProfileType.PRIME_POWER_EDGE_MISSING_DATA;

/**
 * Date: 6/30/13
 * Time: 3:40 PM
 */
@Component
public class PrimePowerProfileResolver implements ProfileResolver {
    public Profile resolve(HorseRace race, Horse horse) {
        ProfileType type = PRIME_POWER_EDGE;
        // Must be turf or synthetic
        if (race.getRace().isTurf()) {
            return null;
        }
        Double pp = getPrimePowerRating(horse.getPrimePowerRating());
        if (pp == null) {
            return null;
        }
        Double maxAmongCompetition = 0.0d;
        int score = 0;
        for (Horse h : race.getHorses()) {
            if (h.equals(horse)) {
                continue;
            }
            Double primePower = getPrimePowerRating(h.getPrimePowerRating());
            if (primePower != null) {
                maxAmongCompetition = Math.max(maxAmongCompetition, primePower);
            } else {
                type = PRIME_POWER_EDGE_MISSING_DATA;
            }
        }
        double difference = pp - maxAmongCompetition;
        if (difference >= 10d) {
            score = 10;
        } else if (difference >= 6d) {
            score = 6;
        } else if (difference >= 3d) {
            score = 3;
        }
        if (score == 0) {
            return null;
        }
        return analysisProfile(type, score);
    }

    private Double getPrimePowerRating(String primePowerRating) {
        try {
            return Double.parseDouble(primePowerRating);
        } catch (Exception e) {
            return null;
        }
    }
}
